<template>
	<view class="content" @click="getuserinfo()">
		<image class="logo" src="/static/logo.png" v-show="!login"></image>
		<image class="logo" :src="userInfo.avatarUrl" v-show="login"></image>
		<view class="text-area">
			<text class="title">{{login ? userInfo.nickName : '点击此处登录'}}</text>
		</view>
	</view>
</template>

<script>
	let _this;
	export default {
		data() {
			return {
				wxcode:'',
				userInfo: {},
				plat:'wxmp',
				login:false,
				userinfo:{},
				openid:''
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			this.plat='wxmp'
			// #endif
			// #ifdef MP-QQ
			this.plat='mpqq'
			// #endif
			//判断当前平台👆
			_this=this
			//本地是否存在用户信息
			uni.getStorage({
				key:"wx_user",
				success:(resstorage)=>{
					if(resstorage.errMsg=="getStorage:ok" &&resstorage.data._id && resstorage.data.avatarUrl){
						console.log("本地已有用户信息👇")
						_this.login=true
						_this.userInfo=resstorage.data
						_this.openid=resstorage.data.openid	
						console.log(_this.userInfo)
						//本地读取用户信息成功
					}else{
						//读取出错或信息有误
						_this.wxlogin()
					}
				},fail() {
					//读取失败，不存在
					_this.wxlogin()
				}
			})
		},
		methods: {
			wxlogin(){
				uni.showLoading({
					mask:true,
					title:"尝试登录中..."
				})
				wx.login({
					success:(loginres)=>{
						_this.wxcode=loginres.code
						console.log(loginres)
						uniCloud.callFunction({
							name:'user_login',
							data:{
								platform:_this.plat,
								code:_this.wxcode,
								appid:uni.getAccountInfoSync().miniProgram.appId
							},
							success(res) {
								console.log(res)
								_this.openid=res.result.openid
								if(res.result.hadlogin==true){
									//云端存在用户信息
									_this.userInfo=res.result.userinfo.data[0]
									console.log(_this.userInfo)
									uni.setStorage({
									    key: 'wx_user',
									    data: _this.userInfo,
									    success: function () {
									        console.log('下载并储存用户信息成功');
											_this.login=true
											_this.openid=_this.userInfo.openid
									    }
									});
									uni.hideLoading()
								}else{
									console.log("用户openid："+_this.openid)
									uni.hideLoading()
									//需用户手动点击授权登录
								}
							},fail() {
								uni.hideLoading()
							}
						})
					}
				})
			},
			getuserinfo(){
				wx.getUserProfile({
					desc: '用于完善用户资料',
					success: (infoRes) => {
						console.log(infoRes)
						if(infoRes.errMsg=="getUserProfile:ok"){
							wx.login({
								success:(loginres)=>{
									_this.wxcode=loginres.code
									uniCloud.callFunction({
										name:'user_login',
										data:{
											platform:"wxmp_add",
											userinfo:infoRes.userInfo,
											openid:_this.openid,
											code:_this.wxcode
										},
										success(cloudres) {
											_this.userInfo=cloudres.result
											console.log(_this.userInfo)
											uni.setStorage({
											    key: 'wx_user',
											    data: _this.userInfo,
											    success: function () {
											        console.log('上传并下载后储存用户信息成功');
													_this.login=true
											    }
											});
										}
									})
								}
							})
						}else{
							console.log("奇怪的错误👆")
						}
					},
					fail(errorres) {
						console.log("getuserprofile错误如下👇")
						console.log(errorres)
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 18rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
